import { Graph, Dom, Addon } from '@antv/x6';

const { Dnd } = Addon;
import axios from 'axios';

import * as nodeApi from '@/apis/bpnmNode.api.js'
function getconfluenceType(shape, nodeData) {
  if (shape == 'start-event') {
    return 15;
  } else if (shape == 'end-event') {
    return 20;
  }
  
  return 1;
  
}

export function createDnd(graph, _options) {
  return new Dnd({
    target: graph,
    scaled: false,
    animation: true,
    getDragNode: (node) => node.clone({ keepId: true }),
    getDropNode: (node) => {
      /* var _node=node;
       var emptyNodeResult = await axios.post('/api1/mesBpmPipeline/createEmptyNode', {
           bpmId: 2,
           nodeName: '开始',
         }).then(res=>res.data);
         if (!(emptyNodeResult && emptyNodeResult.success)) {
           return emptyNodeResult;
         }
         var emptyNode = emptyNodeResult && emptyNodeResult.data;
       _node.id=emptyNode.id
       _node.data = emptyNode;*/
      var _node = node.clone({ keepId: true });
      return _node;
    },
    async validateNode(droppingNode, options) {
      //console.log('droppingNode', droppingNode, droppingNode.shape);
      var emptyNodeResult = undefined;
      var nodeName = droppingNode.data && droppingNode.data.nodeName;
      var emptyNodeId=undefined
      if (droppingNode.shape == 'activity' ||
        droppingNode.shape == 'end-event' ||
        droppingNode.shape == 'start-event') {
        try {
          //createEmptyNode
          //confluence_type     tinyint           not null comment '合流类型   1非合流节点   5合流节点',
          emptyNodeResult = await nodeApi.addNode({
            bpmId: _options.bpmId,
            nodeName: nodeName,
            confluenceType: getconfluenceType(droppingNode.shape, droppingNode.data),
          })
          emptyNodeId = emptyNodeResult&&emptyNodeResult.data;
          console.log('emptyNode', emptyNodeId);
          
          if (!(emptyNodeId)) {
            return Promise.resolve(false);
          }
        } catch (e) {
          console.error(e);
          return Promise.resolve(false);
        }
        
      } else if (droppingNode.shape == 'bpmn-edge') {
        /* emptyNodeResult = await axios.post('/api1/mesBpmPipeline/craeteEmptyEdge', {
           bpmId: 2,
         }).then(res => res.data);
         var emptyNode = emptyNodeResult && emptyNodeResult.data;
         console.log('craeteEmptyEdge', emptyNode, emptyNode && emptyNode.id);
         console.log('最新的id是', emptyNode && emptyNode.id);
         if (!(emptyNode && emptyNode.id)) {
           return Promise.resolve(false);
         }*/
      }
      
      
      sessionStorage.setItem('tempId', emptyNodeId);
      return droppingNode.shape === 'html'
        ? new Promise((resolve) => {
          const { draggingNode, draggingGraph } = options;
          const view = draggingGraph.findView(draggingNode);
          const contentElem = view.findOne('foreignObject > body > div');
          Dom.addClass(contentElem, 'validating');
          setTimeout(() => {
            Dom.removeClass(contentElem, 'validating');
            resolve(true);
          }, 3000);
        })
        : true;
    },
  });
}
